const res = require('express/lib/response')
const db = require('../db/index')
const path = require('path')


// 商品列表
exports.shopList = (req, res) => {
  const sql = `select * from ag_shop`
  db.query(sql, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status:0,
      message:'获取获取商品列表成功!',
      data: results
    })
  })
}

// 添加商品
exports.addShop = (req, res) => {
  const addshopinfo = {
    user_id: req.user.id,
    shop_id: req.body.shop_id
  }
  const sql = `insert into ag_shopping set ?`
  db.query(sql, addshopinfo, (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('添加购物车失败!')
    res.cc('添加购物车成功!', 0)
  })
}


// exports.userShopList = (req, res) => {
//   const sql = `select * from ag_shop where shop_id=?`
//   db.query(sql, req.body.shop_id, (err, results) => {
//     if(err) return res.cc(err)
//     res.send({
//       status:0,
//       message:'获取获取商品列表成功!',
//       data: results
//     })
//   })
// }

// 用户购物车列表
exports.userShopList = (req, res) => {
  // const sql =`select (select * from ag_shop where shop_id=ag_shopping.shop_id) from ag_shopping where user_id=?`
  const sql = `select * from ag_shop a left outer join ag_shopping b on a.shop_id=b.shop_id where b.status=0
                union select * from ag_shop a right outer join ag_shopping b on a.shop_id=b.shop_id where b.status=0`
  db.query(sql,req.user.id, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status:0,
      message:'获取商品列表成功!',
      data: results
    })
  })
}

// 用户购买成功列表
exports.userShopGoodsList = (req, res) => {
  const sql = `select * from ag_shop a left outer join ag_shopping b on a.shop_id=b.shop_id where b.status=1
                union select * from ag_shop a right outer join ag_shopping b on a.shop_id=b.shop_id where b.status=1`
  db.query(sql,req.user.id, (err, results) => {
    if(err) return res.cc(err)
    res.send({
      status:0,
      message:'获取购买记录成功!',
      data: results
    })
  })
}

//  用户购物车信息
exports.userShopInfo = (req, res) => {
  const sql = `select shop_id from ag_shopping where user_id=?`
  db.query(sql, req.user.id, (err,results) => {
    if(err) return res.cc(err)
    res.send({
      status:0,
      message:'获取获取商品信息成功!',
      data: results
    })
  })
}

// 删除购物车内商品
exports.delShopInfo = (req, res) => {
  const sql = `delete from ag_shopping where shop_id=? and user_id=?`
  db.query(sql, [req.body.shop_id,req.user.id], (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('移除商品失败!')
    res.cc('移除商品成功!', 0)
  })
}

// 购买物品
exports.shopGoods = (req, res) => {
  const sql = `update ag_shopping set status=1 where user_id=? and shop_id=?`
  db.query(sql, [req.user.id, req.body.shop_id], (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('购买商品失败!')
    res.cc('购买商品成功!', 0)
  })
}

// 管理员添加商品
exports.addGoodsInfo = (req, res) => {
  const goodsinfo = {
    ...req.body,
    shop_img: path.join(req.file.filename)
  }
  const sql = `insert into ag_shop set ?`
  db.query(sql, goodsinfo, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('发布商品失败！')
    res.cc('发布商品成功！', 0)
  })
}

// 用户购买记录列表
exports.userShoppingList = (req, res) => {
  // const sql = `select * from ag_shop a left outer join ag_shopping b on a.shop_id=b.shop_id 
  //              union select * from ag_shop a right outer join ag_shopping b on a.shop_id=b.shop_id`
  const sql = `select * from ag_shop,ag_shopping,ag_userlogins where ag_shop.shop_id=ag_shopping.shop_id and 
                ag_shopping.user_id=ag_userlogins.user_id and ag_shopping.status=1  order by ag_shopping.shopping_id DESC`
  db.query(sql, (err, results) => {
  if(err) return res.cc(err)
  res.send({
    status:0,
    message:'获取用户购买记录成功!',
    data: results
  })
})
}

// 更改物流状态
exports.changeGoodsStatus = (req, res) => {
  // const goodsstatusinfo = {
  //   ...req.body
  // }
  const sql = `update ag_shopping set shopping_status=? where shopping_id=?`
  db.query(sql, [req.body.shopping_status,req.body.shopping_id], (err, results) => {
    if(err) return res.cc(err)
    if(results.affectedRows !== 1) return res.cc('修改状态失败!')
    res.cc('修改状态成功!', 0)
  })
}

// 下架商品
exports.delGoodsInfo = (req, res) => {
  const sql = `delete from ag_shop where shop_id=?`
  db.query(sql, req.body.shop_id, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('下架商品失败！')
    res.cc('下架商品成功！', 0)
  })
}